package base.http.utils;

import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;


/**
 * Created by LZH On 2019/3/15
 */
public class BaseDialog extends AlertDialog {

    private int layoutResID;
    private View parentView;
    protected Context mContext;
    private Window mWindow;



    /**
     * @param context     Context
     * @param layoutResID dialog layout
     */
    public BaseDialog(@NonNull Context context, int layoutResID) {
        super(context);
        mContext = context;
        this.layoutResID = layoutResID;
    }

    /**
     * @param context     Context
     * @param layoutResID dialog layout
     * @param theme       theme id
     */
    public BaseDialog(@NonNull Context context, int layoutResID, int theme) {
        super(context, theme);
        mContext = context;
        this.layoutResID = layoutResID;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        parentView = getLayoutInflater().inflate(layoutResID, null);
        setContentView(parentView);
        mWindow = getWindow();
        getWindow().getDecorView().setPadding(0, 0, 0, 0);
        mWindow.setBackgroundDrawableResource(android.R.color.transparent);
//        //解决 状态栏变色的bug
//        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
//            mWindow.addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGRe OUNDS);
//            mWindow.addFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS);
//            mWindow.setStatusBarColor(Color.TRANSPARENT);
//            if(Build.VERSION.SDK_INT >= Build.VERSION_CODES.N){
//                try {
//                    Class decorViewClazz = Class.forName("com.android.internal.policy.DecorView");
//                    Field field = decorViewClazz.getDeclaredField("mSemiTransparentStatusBarColor");
//                    field.setAccessible(true);
//                    field.setInt(getWindow().getDecorView(), Color.TRANSPARENT);  //去掉高版本蒙层改为透明
//                } catch (Exception e) {}
//            }
//        }
    }

    public void setDialogWH(int width, int height) {
        if (mWindow == null) mWindow = getWindow();
        mWindow.setLayout(width, height);
    }

    public void setDialogXY(int x, int y) {
        if (mWindow == null) mWindow = getWindow();
        if (mWindow != null) {
            WindowManager.LayoutParams attributes = mWindow.getAttributes();
            attributes.x = x;
            attributes.y = y;
            mWindow.setAttributes(attributes);
        }
    }

    public void setGravity(int gravity) {
        if (mWindow == null) mWindow = getWindow();
        mWindow.setGravity(gravity);
    }

    public void setAlpha(float alpha) {
        if (mWindow == null) mWindow = getWindow();
        WindowManager.LayoutParams lp = mWindow.getAttributes();
        lp.alpha = alpha;
        mWindow.setAttributes(lp);
    }


    public <T extends View> T findViewById(int id) {
        return parentView.findViewById(id);
    }



    public View getContentView(){
        return parentView;
    }

}
